<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Theme extends Controller_DefaultTemplate{

public function action_index()
{
}

public function action_add()
{
	if (Auth::instance()->logged_in('admin'))
	{
		$this->template->content = View::factory('admin/theme_add');
	}
}

public function action_edit()
{
	if (Auth::instance()->logged_in('admin'))
	{
		$theme = ORM::factory('theme')->find_all();
		$this->template->content = View::factory('admin/theme_edit');
		$this->template->content->set('theme_list', $theme->as_array());
	}
}

public function action_delete()
{
	if (Auth::instance()->logged_in('admin'))
	{
		$id = $this->request->param('id');
		$theme = ORM::factory('theme')->where('id', '=', $id)->find();
		if ($theme->loaded())
		{
			$theme->delete();
			$this->request->redirect('admin/theme/edit');
		}
	
	}
}

public function action_modify()
{
	if (Auth::instance()->logged_in('admin'))
	{
		$id = $this->request->param('id');
		$theme = ORM::factory('theme')->where('id', '=', $id)->find();
		if ($theme->loaded())
		{
			$this->template->content = View::factory('admin/theme_modify');
			$this->template->content->set('theme', $theme);
		}
	
	}
}

public function action_modify_save()
{
	if ($_POST)
    {
		$id = arr::get($_POST, 'theme_id');
		$theme = ORM::factory('theme')->where('id', '=', $id)->find();
		if ($theme->loaded())
		{
			$theme->values(array(
				'name' => arr::get($_POST, 'theme_name'),
				'title' => arr::get($_POST, 'theme_title'),
				'price_rmb' => arr::get($_POST, 'price_rmb'),
				'price_usd' => arr::get($_POST, 'price_usd'),
				'score' => arr::get($_POST, 'score'),
				'apk_url' => arr::get($_POST, 'filePath'),
				'preview_url' => arr::get($_POST, 'filePath_img'),
				'description' => arr::get($_POST, 'descriptions'),
				'update_time' => time(),
			));
			$theme->save();
			echo 'suc';
			return;
		}

	}
	echo 'fail';
	return;
}

public function action_save()
{
	if ($_POST)
    {
		$theme = ORM::factory('theme');
		$theme->values(array(
			'name' => arr::get($_POST, 'theme_name'),
		    'title' => arr::get($_POST, 'theme_title'),
			'price_rmb' => arr::get($_POST, 'price_rmb'),
			'price_usd' => arr::get($_POST, 'price_usd'),
			'score' => arr::get($_POST, 'score'),
			'apk_url' => arr::get($_POST, 'filePath'),
			'preview_url' => arr::get($_POST, 'filePath_img'),
			'description' => arr::get($_POST, 'descriptions'),
			'create_time' => time(),
		));
		$theme->save();
		echo 'suc';
		return;
	}
	echo 'fail';
	return;
}

}

